{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ba565a12",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 1. 华氏度&摄氏度转换\n",
    "# 定义函数getCelsius,将华氏温度转化为摄氏温度\n",
    "def getCelsius(f):\n",
    "    c= (f - 32) * 5 / 9  # 计算摄氏温度\n",
    "    return c  # 返回摄氏温度 \n",
    "\n",
    "f = float(input())  # 输入华氏温度\n",
    "c = getCelsius(f)   # 调用getCelsius求摄氏温度\n",
    "print(\"fahr=%.1f, celsius = %.1f\\n\"%(f, c)); # 输出结果"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "56ad785b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "完全平方数有 [1, 4]\n"
     ]
    }
   ],
   "source": [
    "# 2. 完全平方数\n",
    "def filterLst(L):\n",
    "    outL=[]\n",
    "    for x in L:\n",
    "        for i in range(x + 1):\n",
    "            if i * i == x:\n",
    "                outL.append(x)\n",
    "    return outL\n",
    "\n",
    "s = input()\n",
    "data = s.split(\",\")\n",
    "for i in range(len(data)):\n",
    "    data[i] = int(data[i])\n",
    "\n",
    "result = filterLst(data)\n",
    "\n",
    "if len(result)!=0:\n",
    "    print (\"完全平方数有\", result)\n",
    "else:\n",
    "    print (\"无符合要求的数！\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "01e635e7",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2000年是闰年\n"
     ]
    }
   ],
   "source": [
    "# 3. 判断闰年\n",
    "def is_leap(year):\n",
    "    \"\"\"接收一个表示年份的正整数，判断该年是否为闰年，返回布尔值\"\"\"\n",
    "    if (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0):\n",
    "        return True\n",
    "    else:\n",
    "        return False\n",
    "\n",
    "if __name__ == '__main__':\n",
    "    year_int = int(input())\n",
    "    if is_leap(year_int):\n",
    "        print(f'{year_int}年是闰年')\n",
    "    else:\n",
    "        print(f'{year_int}年不是闰年')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "6b84e042",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "斐波那契数列第10项的值是：55\n"
     ]
    }
   ],
   "source": [
    "# 4. 斐波那契数列\n",
    "previous = {0:0, 1:1} # 字典\n",
    "def fibonacci_s(n):\n",
    "    if n in previous.keys():\n",
    "        return previous[n]\n",
    "    else:\n",
    "        newValue = fibonacci_s(n - 1) + fibonacci_s(n - 2)\n",
    "        previous[n] = newValue\n",
    "        return newValue\n",
    "a=int(input())\n",
    "print('斐波那契数列第%d项的值是：%d'%(a, fibonacci_s(a)))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0865c8b1",
   "metadata": {},
   "source": [
    "**<font color = 'red'>以下算法请注意！！</font>**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "e77148bb",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0,1,1,2,3,5,8,13,21,34,55,89,144,233,377,610,987,1597,2584,4181,"
     ]
    }
   ],
   "source": [
    "# 5. 打印输出前20个斐波那契数\n",
    "def fib(n):\n",
    "    a = 0\n",
    "    b = 1\n",
    "    dic = dict() # 定义字典        \n",
    "    for i in range(n):     \n",
    "        dic[i] = a # 添加斐波那契数到字典\n",
    "        tmp = a\n",
    "        a = b\n",
    "        b = tmp + b\n",
    "    return dic\n",
    "    \n",
    "# 调用函数生成斐波那契数列中的前20个斐波那契数\n",
    "fibonac = fib(20)       \n",
    "for key in fibonac.keys():\n",
    "    print(fibonac[key], end=\",\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "dbfbecdb",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2 3 5 "
     ]
    }
   ],
   "source": [
    "# 6. 判断素数\n",
    "import math\n",
    "def isPrime(n):          # 定义判断素数的函数\n",
    "    if n < 2:\n",
    "        return False\n",
    "    elif n == 2:\n",
    "        return True\n",
    "    for i in range(2, int(math.sqrt(n)) + 1): # 复杂度O(sqrt(n))\n",
    "        if n % i == 0:\n",
    "            return False\n",
    "    else:\n",
    "        return True\n",
    "    \n",
    "num = int(input())       # 接收用户输入并转成整数\n",
    "for n in range(num + 1):\n",
    "    if isPrime(n):\n",
    "        print(n, end=' ')"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": ".venv",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.13.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
